package com.jier.service.impl;

import com.jier.common.Page;
import com.jier.common.Result;
import com.jier.common.util.StringUtil;
import com.jier.dao.SystemModuleDao;
import com.jier.entities.SystemModule;
import com.jier.service.ISystemModuleService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 系统模块实现
 *
 * @author jie_r
 * @date 2018/03/08
 */
@Service
public class SystemModuleServiceImpl extends BaseServiceImpl<SystemModuleDao, SystemModule> implements ISystemModuleService {
    @Override
    public List<SystemModule> getFirstLevelNaves() throws Exception {
        Example example = getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parent", 0);
        criteria.andEqualTo("status", Boolean.TRUE);
        example.setOrderByClause("sort_order asc");
        return getByExample(example);
    }

    @Override
    public List<SystemModule> getSecondLevelNaves() throws Exception {
        Example example = getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("parent", 0);
        criteria.andEqualTo("status", Boolean.TRUE);
        example.setOrderByClause("sort_order asc");
        return getByExample(example);
    }

    @Override
    public List<SystemModule> getFirstLevelNavesByRoleId(Integer systemRoleId) throws Exception {
        return mapper.getFirstLevelNavesByRoleId(systemRoleId);
    }

    @Override
    public List<SystemModule> getSecondLevelNavesByRoleId(Integer systemRoleId) throws Exception {
        return mapper.getSecondLevelNavesByRoleId(systemRoleId);
    }

    @Override
    public Result<SystemModule> getSystemParentModule(Page page) throws Exception {
        return getRecordPage("getSystemParentModule", page, null);
    }

    @Override
    public List<SystemModule> getSystemChildModule(Integer parentId) throws Exception {
        return mapper.getSystemChildModule(parentId);
    }

    @Override
    public SystemModule getLastSystemModule(Integer parent) throws Exception {
        Example example = getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parent", parent);
        example.setOrderByClause("sort_order desc");
        List<SystemModule> systemModules = getByExample(example);
        if (StringUtil.isEmptyList(systemModules)) {
            return null;
        }
        return systemModules.get(0);
    }

    @Override
    public SystemModule getSystemModuleByNumber(String number) throws Exception {
        Example example = getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("number", number);
        List<SystemModule> systemModules = getByExample(example);
        if (StringUtil.isEmptyList(systemModules)) {
            return null;
        }
        return systemModules.get(0);
    }

    @Override
    public SystemModule getSystemModuleByName(String name) throws Exception {
        Example example = getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name", name);
        List<SystemModule> systemModules = getByExample(example);
        if (StringUtil.isEmptyList(systemModules)) {
            return null;
        }
        return systemModules.get(0);
    }

    @Override
    public List<SystemModule> getSystemModuleField() throws Exception {
        Example example = getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("parent", 0);
        example.setOrderByClause("parent, sort_order");
        return getByExample(example);
    }

    @Override
    public SystemModule getSystemModuleByPermissionCode(String permissionCode) throws Exception {
        List<SystemModule> systemModules = mapper.getSystemModuleByPermissionCode(permissionCode);
        if (StringUtil.isEmptyList(systemModules)) {
            return null;
        }
        return systemModules.get(0);
    }
}
